<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pvdo extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $all_session = $this->session->all_userdata();

    
        if($all_session['login'][0]['mem_type'] != 2 && $all_session['login'][0]['mem_type'] != 1)
        {
        	redirect('authen');
        } 

    }
    
	public function index()
	{		
		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pvdo/index';
		// $config['total_rows'] = count($this->db->get('vdo')->result_array());
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);
		

		//////////////////////////////////////////////////////
		///////////////////// result by //////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('result_by') == null)
		{
			$result_by = 'vdo.vdo_id';

			if(@$all_session['result_by'] != null)
			{
				$result_by = $all_session['result_by'];
			}
			
		} else {
			$result_by = $this->input->post('result_by');
			$session['result_by'] = $result_by;
			$this->session->set_userdata($session);

		}

		//////////////////////////////////////////////////////
		//////////////////// category by /////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('category_by') == null)
		{
			$category_by = 'all';

			if(@$all_session['category_by'] != null)
			{
				$category_by = $all_session['category_by'];
			}

		} else {
			$category_by = $this->input->post('category_by');			
			$session['category_by'] = $category_by;
			$this->session->set_userdata($session);
		}

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////
		
 		$mem_id = $all_session['login'][0]['mem_id'];
 		$mem_type = $all_session['login'][0]['mem_type'];

		$config['total_rows'] = count($this->pvdo_model->count_vdo($result_by ,$category_by ,$mem_id ,$mem_type));

		$data['vdo'] = $this->pvdo_model->count_vdo($result_by ,$category_by ,$mem_id ,$mem_type);

		
		$data['total_rows'] = $config['total_rows'];

		$data['sum_public'] = 0;
		$data['sum_private'] = 0;
		$data['sum_ban'] = 0;
		
		
		if($all_session['login'][0]['mem_type'] == 1 )
		{
			$data['all'] = count($data['vdo']);
			for($i=0;$i<$data['all'];$i++)
			{
				if($data['vdo'][$i]['mem_id'] == $all_session['login'][0]['mem_id'])
				{
					if($data['vdo'][$i]['vdo_status'] == '0')
					{
						$data['sum_public']+=1;
					} else if ($data['vdo'][$i]['vdo_status'] == 1) {
						$data['sum_private']+=1;
					} else if ($data['vdo'][$i]['vdo_status'] == null) {
						$data['sum_ban']+=1;
					}
				}
					
			}
			

		} else {

			$data['all'] = count($data['vdo']);
			for($i=0;$i<$data['all'];$i++)
			{
				if($data['vdo'][$i]['vdo_status'] == '0')
				{
					$data['sum_public']+=1;
				} else if ($data['vdo'][$i]['vdo_status'] == 1) {
					$data['sum_private']+=1;
				} else if ($data['vdo'][$i]['vdo_status'] == null) {
					$data['sum_ban']+=1;
				}
			}

		}
		$data['all'] = $config['total_rows'];


		$data['category'] = $this->pvdo_model->get_category();

		$this->pagination->initialize($config); 

		$data['vdo'] = $this->pvdo_model->get_vdo($per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type);
		$data['total_rows'] = count($data['vdo']);
		
		$this->load->view('pvdo',$data);
	}

	public function search()
	{
		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pvdo/search';
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);

		//////////////////////////////////////////////////////
		//////////////////////// search //////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('search') == null)
		{
			if(@$all_session != null)
			{
				$input = array(
					'search' => @$all_session['search']
				);
			}

		} else {			
			$session['search'] = $this->input->post('search');
			$this->session->set_userdata($session);

			$input = array(
				'search' => $this->input->post('search')
			);
		}

		//////////////////////////////////////////////////////
		///////////////////// result by //////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('result_by') == null)
		{
			$result_by = 'vdo.vdo_id';

			if(@$all_session['result_by'] != null)
			{
				$result_by = $all_session['result_by'];
			}
			
		} else {
			$result_by = $this->input->post('result_by');
			$session['result_by'] = $result_by;
			$this->session->set_userdata($session);				

		}

		//////////////////////////////////////////////////////
		//////////////////// category by /////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('category_by') == null)
		{
			$category_by = 'all';

			if(@$all_session['category_by'] != null)
			{
				$category_by = $all_session['category_by'];
			}

		} else {
			$category_by = $this->input->post('category_by');			
			$session['category_by'] = $category_by;
			$this->session->set_userdata($session);
			
		}

		

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////

		$mem_id = $all_session['login'][0]['mem_id'];
		$mem_type = $all_session['login'][0]['mem_type'];


		$data['vdo'] = $this->pvdo_model->count_search($input ,$result_by ,$category_by ,$mem_id ,$mem_type);
		
		$config['total_rows'] = count($this->pvdo_model->count_search($input ,$result_by ,$category_by ,$mem_id ,$mem_type));



		$data['all'] = count($data['vdo']);
		$data['total_rows'] = $config['total_rows'];

		$data['sum_public'] = 0;
		$data['sum_private'] = 0;
		$data['sum_ban'] = 0;
		for($i=0;$i<$data['all'];$i++)
		{
			if($data['vdo'][$i]['vdo_status'] == '0')
			{
				$data['sum_public']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == 1) {
				$data['sum_private']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == null) {
				$data['sum_ban']+=1;
			}
		}


		$data['category'] = $this->pvdo_model->get_category();
		$this->pagination->initialize($config);

		$data['vdo'] = $this->pvdo_model->search($input ,$per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type);
		$data['total_rows'] = count($data['vdo']);

		$this->load->view('pvdo',$data);
	}

	public function approve()
	{		
		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pvdo/approve';
		// $config['total_rows'] = count($this->db->get('vdo')->result_array());
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);
		

		//////////////////////////////////////////////////////
		///////////////////// result by //////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('result_by') == null)
		{
			$result_by = 'vdo.vdo_id';

			if(@$all_session['result_by'] != null)
			{
				$result_by = $all_session['result_by'];
			}
			
		} else {
			$result_by = $this->input->post('result_by');
			$session['result_by'] = $result_by;
			$this->session->set_userdata($session);

		}

		//////////////////////////////////////////////////////
		//////////////////// category by /////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('category_by') == null)
		{
			$category_by = 'all';

			if(@$all_session['category_by'] != null)
			{
				$category_by = $all_session['category_by'];
			}

		} else {
			$category_by = $this->input->post('category_by');			
			$session['category_by'] = $category_by;
			$this->session->set_userdata($session);
		}

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////

		$mem_id = $all_session['login'][0]['mem_id'];
		$mem_type = $all_session['login'][0]['mem_type'];
 
		$config['total_rows'] = count($this->pvdo_model->count_approve($result_by ,$category_by ,$mem_id ,$mem_type));

		$data['vdo'] = $this->pvdo_model->count_approve($result_by ,$category_by ,$mem_id ,$mem_type);
		
		$data['total_rows'] = $config['total_rows'];

		$data['sum_public'] = 0;
		$data['sum_private'] = 0;
		$data['sum_ban'] = 0;
		
		
		if($all_session['login'][0]['mem_type'] == 1 )
		{
			$data['all'] = count($data['vdo']);
			for($i=0;$i<$data['all'];$i++)
			{
				if($data['vdo'][$i]['mem_id'] == $all_session['login'][0]['mem_id'])
				{
					if($data['vdo'][$i]['vdo_status'] == '0')
					{
						$data['sum_public']+=1;
					} else if ($data['vdo'][$i]['vdo_status'] == 1) {
						$data['sum_private']+=1;
					} else if ($data['vdo'][$i]['vdo_status'] == null) {
						$data['sum_ban']+=1;
					}
				}
					
			}
			

		} else {

			$data['all'] = count($data['vdo']);
			for($i=0;$i<$data['all'];$i++)
			{
				if($data['vdo'][$i]['vdo_status'] == '0')
				{
					$data['sum_public']+=1;
				} else if ($data['vdo'][$i]['vdo_status'] == 1) {
					$data['sum_private']+=1;
				} else if ($data['vdo'][$i]['vdo_status'] == null) {
					$data['sum_ban']+=1;
				}
			}

		}
		$data['all'] = $config['total_rows'];


		$data['category'] = $this->pvdo_model->get_category();

		$this->pagination->initialize($config); 

		$data['vdo'] = $this->pvdo_model->get_approve($per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type);
		$data['total_rows'] = count($data['vdo']);
		
		$this->load->view('pvdo_approve',$data);
	}

	public function search_approve()
	{
		$all_session = $this->session->all_userdata();
		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/pvdo/search_approve';
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);

		//////////////////////////////////////////////////////
		//////////////////////// search //////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('search') == null)
		{
			if(@$all_session != null)
			{
				$input = array(
					'search' => @$all_session['search']
				);
			}

		} else {			
			$session['search'] = $this->input->post('search');
			$this->session->set_userdata($session);

			$input = array(
				'search' => $this->input->post('search')
			);
		}

		//////////////////////////////////////////////////////
		///////////////////// result by //////////////////////
		//////////////////////////////////////////////////////

		if($this->input->post('result_by') == null)
		{
			$result_by = "`vdo`.`vdo_id`";

			if(@$all_session['result_by'] != null)
			{
				$result_by = $all_session['result_by'];
			}
			
		} else {
			$result_by = $this->input->post('result_by');
			$session['result_by'] = $result_by;
			$this->session->set_userdata($session);				

		}

		//////////////////////////////////////////////////////
		//////////////////// category by /////////////////////
		//////////////////////////////////////////////////////
		if($this->input->post('category_by') == null)
		{
			$category_by = 'all';

			if(@$all_session['category_by'] != null)
			{
				$category_by = $all_session['category_by'];
			}

		} else {
			$category_by = $this->input->post('category_by');			
			$session['category_by'] = $category_by;
			$this->session->set_userdata($session);
			
		}

		

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////
		$mem_id = $all_session['login'][0]['mem_id'];
		$mem_type = $all_session['login'][0]['mem_type'];

		$data['vdo'] = $this->pvdo_model->count_search_approve($input ,$result_by ,$category_by ,$mem_id ,$mem_type);
		
		$config['total_rows'] = count($this->pvdo_model->count_search_approve($input ,$result_by ,$category_by ,$mem_id ,$mem_type));

	
		// print_r($data['vdo']);



		$data['all'] = count($data['vdo']);
	

		$data['sum_public'] = 0;
		$data['sum_private'] = 0;
		$data['sum_ban'] = 0;
		for($i=0;$i<$data['all'];$i++)
		{
			if($data['vdo'][$i]['vdo_status'] == '0')
			{
				$data['sum_public']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == 1) {
				$data['sum_private']+=1;
			} else if ($data['vdo'][$i]['vdo_status'] == null) {
				$data['sum_ban']+=1;
			}
		}


		$data['category'] = $this->pvdo_model->get_category();
		$this->pagination->initialize($config);

		$data['vdo'] = $this->pvdo_model->search_approve($input ,$per_page ,$uri ,$result_by ,$category_by ,$mem_id ,$mem_type);
		$data['total_rows'] = count($data['vdo']);

		$this->load->view('pvdo_approve',$data);
	}

	public function profile()
	{
		$all_session = $this->session->all_userdata();
		
		$id = $this->uri->segment(3);
		$data['profile'] = $this->pvdo_model->get_profile($id);
		$data['thumb'] = $this->pvdo_model->get_thumb($id);
		$data['category'] = $this->pvdo_model->get_category();

		if($all_session['login'][0]['mem_type'] == 1 && $all_session['login'][0]['mem_type'] == $data['profile'][0]['mem_id'])
		{
			redirect('pvdo');
		}
	
		$this->load->view('pvdo_profile',$data);
	}

	public function update_profile()
	{
		$thumb_select = $this->input->post('options');
		$vdo_id = $this->uri->segment(3);
		$data['thumb'] = $this->pvdo_model->get_thumb($vdo_id);

		if($this->input->post('options') != null)
		{
			for($i=0;$i<count($data['thumb']);$i++)
			{
				if($i == $thumb_select)
				{
					$thumb = array(
						'thumb_id' => $data['thumb'][$i]['thumb_id'],
						'thumb_choose' => 1
					);

					$this->pvdo_model->update_thumb($thumb);

				} else {

					$thumb = array(
						'thumb_id' => $data['thumb'][$i]['thumb_id'],
						'thumb_choose' => 0
					);
					$this->pvdo_model->update_thumb($thumb);
				}
			}
		}

		if($this->input->post('vdo_status') == '')
		{
			$vdo_status = null;
		} else {
			$vdo_status = $this->input->post('vdo_status');
		}
		$input = array(
			'vdo_id' => $this->uri->segment(3),
			'vdo_name' => $this->input->post('vdo_name'),
			'vdo_des' => str_replace("\n", "<br>\n", $this->input->post('vdo_des')),
			'vdo_status' => $vdo_status,		
			'vdo_tag' => $this->input->post('vdo_tag'),
			'category_id' => $this->input->post('category_id'),
			'event_name' => $this->input->post('event_name'),
			'event_start' => $this->input->post('event_start'),
			'event_end' => $this->input->post('event_end')
		);
		$this->pvdo_model->update_profile($input);

		////////////////////////////////////////////////////
		////////////////////// Log File ////////////////////
		////////////////////////////////////////////////////

		$all_session = $this->session->all_userdata();

		if($this->input->post('vdo_name') != '')
		{
			$log_file = array(						
				'log_url' => base_url().'index.php/pvdo/profile/'.$input['vdo_id'],
				'log_action' => 'Edit Vdo',
				'log_target' => $input['vdo_name'],
				'log_status' => 'Pass',
				'mem_id' => $all_session['login'][0]['mem_id'],
				'log_ip' => $_SERVER['REMOTE_ADDR']						
			);
			$this->logfile_model->add_logfile($log_file);

		}
		
		////////////////////////////////////////////////////
		////////////////// check_delete vdo ////////////////
		////////////////////////////////////////////////////
		$id = $this->uri->segment(3);
		$data['profile'] = $this->pvdo_model->get_profile($id);

		$check_delete = $this->input->post('check_delete');
		if ($check_delete != null)
		{
			$log_file = array(						
				'log_url' => base_url().'index.php/pvdo/profile/'.$input['vdo_id'],
				'log_action' => 'Delete Vdo',
				'log_target' => $input['vdo_name'],
				'log_status' => 'Pass',
				'mem_id' => $all_session['login'][0]['mem_id'],
				'log_ip' => $_SERVER['REMOTE_ADDR']						
			);
			sleep(1);
			$this->logfile_model->add_logfile($log_file);

			$delete = array(
				'vdo_id' => $data['profile'][0]['vdo_id'],
				'server_ip' => $data['profile'][0]['server_ip'],
				'ftp_name' => $data['profile'][0]['ftp_name'],
				'ftp_pass' => $data['profile'][0]['ftp_pass'],
				'vdo_rawfile' => $data['profile'][0]['vdo_rawfile']
			);

			$this->delete_vdo($delete);
			redirect('pvdo');

		} else {
			redirect('pvdo/profile/'.$input['vdo_id']);
		}
		
	}	

	public function delete_vdo($delete)
	{

		$config['hostname'] = $delete['server_ip'];
		$config['username'] = $delete['ftp_name'];
		$config['password'] = base64_decode($delete['ftp_pass']);
		$config['debug']	= TRUE;
		$this->ftp->connect($config);

		$this->ftp->delete_file($delete['vdo_rawfile']);

		$vdo_rawfile = iconv("utf-8","tis-620",$delete['vdo_rawfile']);	
		delete_files('./vod_thumb/'.$vdo_rawfile);			
		rmdir('./vod_thumb/'.$vdo_rawfile);


		$this->delete_vdo_model->delete_vdo($delete['vdo_id']);
		$this->delete_vdo_model->delete_like($delete['vdo_id']);
		$this->delete_vdo_model->delete_dislike($delete['vdo_id']);
		// $this->delete_vdo_model->delete_slide($delete['vdo_id']);
		$this->delete_vdo_model->delete_vdothumb($delete['vdo_id']);

	}

	public function upload()
	{
		$data['server'] = $this->pvdo_model->get_server();
		$this->load->view('pvdo_upload' ,$data);
	}

	public function category()
	{
		$data['category'] = $this->category_model->get_category();
		$data['edit'] = $data['category'];
		$this->load->view('pvdo_category',$data);
	}
	
	public function new_category()
	{
		$input = array(
			'category_name' => $this->input->post('category_name')
		);

		$this->category_model->new_category($input);
		redirect('pvdo/category');
	}

	public function update_category()
	{
		$input = array(
			'category_id' => $this->uri->segment(3),
			'category_name' => $this->input->post('category_name')
		);

		$this->category_model->update_category($input);
		redirect('pvdo/category');
	}

	public function delete_category()
	{
		$category_id = $this->uri->segment(3);
		$this->category_model->delete_category($category_id);
		redirect('pvdo/category');
	}

	public function update_status()
	{
		
		$select_all = $this->input->post('select_all');
		$input = array(
			'vdo_status' => $this->input->post('status')
		);


		for($i=0;$i<count($select_all);$i++)
		{
			if($select_all[$i] != "")
			{
				$id = $select_all[$i];
				$this->pvdo_model->update_status($id ,$input);
			}
		}

		redirect('pvdo');
	}

	public function change_category()
	{
		$id = $this->uri->segment(3);
		$data['check_vdoid'] = $this->pvdo_model->check_change_category($id);

		$input = array(
			'category_id' => $this->input->post('category_id')
		);


		for($i=0;$i<count($data['check_vdoid']);$i++)
		{
			$this->pvdo_model->change_category($data['check_vdoid'][$i]['vdo_id'] ,$input);
		}

		$this->pvdo_model->delete_category($id);
		redirect('pvdo/category');

	}

	public function custom_thumbnail()
	{
		
		$vdo_rawfile = $this->input->post('rawfile');

		////////////////////////////////////////////
		////// find date for mem_dateregister //////
		////////////////////////////////////////////
		$datestring = "%Y-%m-%d";
		$time = time();
		
		////////////////////////////////////////////
		//////////////// upload file ///////////////
		////////////////////////////////////////////

		$path = strtolower($_FILES["userfile"]['name']);
		$file_type = pathinfo($path, PATHINFO_EXTENSION);

		$thumb_name = uniqid().'_custom.'.$file_type;
		$thumb_name = iconv("utf-8","tis-620",$thumb_name);

		
		$vdo_rawfile = iconv("utf-8","tis-620",$vdo_rawfile);

		

		if($file_type == 'jpeg' || $file_type == 'gif' || $file_type == 'png' || $file_type == 'jpg')
		{
			move_uploaded_file($_FILES["userfile"]["tmp_name"],"vod_thumb/".$vdo_rawfile."/".$thumb_name);

		}
		
		////////////////////////////////////////////
		//////////////// input field ///////////////
		////////////////////////////////////////////
		$vdo_id = $this->input->post('id');		
		$data['thumb'] = $this->pvdo_model->get_thumb($vdo_id);

		if( count($data['thumb']) <= 3 )
		{
			$input = array(
				'thumb_name' => $thumb_name,
				'vdo_id' => $vdo_id,
				'thumb_choose' => 1
			);
			$this->pvdo_model->add_thumb($input);
		} else {
			$input = array(
				'thumb_id' => $data['thumb'][3]['thumb_id'],
				'thumb_name' => $thumb_name,
				'thumb_choose' => 1
			);
			$this->pvdo_model->update_thumb($input);
		}
		

		for($i=0;$i<count($data['thumb'])-1;$i++)
		{
			$input = array(
				'thumb_id' => $data['thumb'][$i]['thumb_id'],
				'thumb_choose' => 0
			);
			$this->pvdo_model->update_thumb($input);
		}

		$this->load->library('crop');
		$this->crop->cropImage(640, 360, './vod_thumb/'.$vdo_rawfile.'/'.$thumb_name, $file_type, './vod_thumb/'.$vdo_rawfile.'/'.$thumb_name);

		redirect('pvdo/profile/'.$vdo_id);
	}
}



